package com.fc.im.netty;

import com.alibaba.fastjson.JSONObject;
import com.fc.im.netty.message.Message;
import com.fc.im.netty.message.MessageType;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * Created by ChanJiabo on 2017/1/18.
 */
public class ServerMessageHandler extends SimpleChannelInboundHandler<Message>  {

    private static final String DEFAULT_ENCODE = "UTF-8";


    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        super.exceptionCaught(ctx, cause);
        ctx.close();
    }

    @Override
    protected void messageReceived(ChannelHandlerContext ctx, Message msg) throws Exception {
        Channel channel = ctx.channel();
        if (msg == null) {
            return;
        }
        try{
            int msgType = msg.getHeader().getMsgType();
            if (msgType == MessageType.HEART_BEAT) {
                channel.writeAndFlush(Message.heartBeatMessage());
                return;
            }
            JSONObject requestBody = JSONObject.parseObject(new String(msg.getBody(), DEFAULT_ENCODE));
            int module = requestBody.getIntValue()
        }
    }
}
